﻿<%@ WebHandler Language="C#" Class="RbTechniqueStatusReport" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Net;
using ReportBusiness;

/**
 * Description: 技术状态查询报表
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * Date:2025.4.17
 **/

public class RbTechniqueStatusReport : IHttpHandler
{
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getData":
                        r = getData(jObject);//查询
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "exportExcel":
                        exportExcel(jObject);//导出
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    string getSearchSqlString(JObject jObject)
    {
        string sql = @"
SELECT cci.productname,cci.productdescription,cci.containername,cci.originalstartdate,
       p.materialtrademark,cci.familylistnames,
       ws.workflowstepname,
       spi.stepno || '-' || spi.description stepinfoname,
       sc.checkpointnotedis_b,
       sc.drawingrequiredis_b,
       scr.checkpointresultnotedis_b,
       scr.checkdate,
       wf.workflowtype,
       wf.process_no,
       p.productrevision,
       wf.part_revid,
       NULL workflow_mark,--工艺规程标识,
       NULL workflow_rev, --工艺规程版次,
       NULL ischange,     --是否涉及工艺更改,
       NULL changeno,     --更改单号,
       NULL workflow_expire_date,  --临时工艺有效期,
       DECODE(sc.keycheck,1,'是','否') iskeycheck,
       smp.specialcoursespecreportno,
       sm.createdate confirmdate,--特殊过程确认时间,
       CASE WHEN smp.indate IS NOT NULL THEN add_months(smp.indate,60) 
            ELSE NULL 
       END special_expire_date,--特殊过程到期时间
       ws.chargingqty
FROM stepcheckpoint sc
LEFT JOIN stepcheckpointresult scr ON scr.stepcheckpointid = sc.stepcheckpointid
LEFT JOIN workflowstep ws ON ws.workflowstepid = sc.workflowstepid
LEFT JOIN stepinfo spi ON spi.stepinfoid = sc.stepinfoid
INNER JOIN containercurrentinfo cci ON cci.containerid = scr.containerid
LEFT JOIN product p ON p.productid = cci.productid
LEFT JOIN workflow wf ON wf.workflowid = ws.workflowid
LEFT JOIN specialcoursespecmapp sm ON sm.workflowstepid = sc.workflowstepid
LEFT JOIN specialcoursespecreport smp ON smp.specialcoursespecreportid = sm.specialcoursespecreportid
WHERE 1=1";
        //主制分厂
        if (jObject["factoryid"] != null && jObject["factoryid"].ToString() != "")
        {
            sql += string.Format(" AND cci.originalfactoryid = '{0}'", jObject["factoryid"].ToString());
        }

        //零件号
        if (jObject["productname"] != null && jObject["productname"].ToString() != "")
        {
            sql += string.Format(" AND cci.productname = '{0}'", jObject["productname"].ToString());
        }

        //批次号
        if (jObject["containername"] != null && jObject["containername"].ToString() != "")
        {
            sql += string.Format(" AND cci.containername = '{0}'", jObject["containername"].ToString());
        }

        //工序名称
        if (jObject["workflowstepname"] != null && jObject["workflowstepname"].ToString() != "")
        {
            sql += string.Format(" AND ws.workflowstepname LIKE '%{0}%'", jObject["workflowstepname"].ToString());
        }

        //机型
        if (jObject["productfamilyname"] != null && jObject["productfamilyname"].ToString() != "")
        {
            sql += string.Format(" AND cci.familylistnames = '{0}'", jObject["productfamilyname"].ToString());
        }

        //开卡时间
        if (jObject["containerstartdate"] != null && jObject["containerstartdate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["containerstartdate"].ToString());
            sql += string.Format(" AND cci.originalstartdate >= TO_DATE('{0} 00:00:00','yyyy-mm-dd hh24:mi:ss')", arr[0].ToString());
            sql += string.Format(" AND cci.originalstartdate <= TO_DATE('{0} 23:59:59','yyyy-mm-dd hh24:mi:ss')", arr[1].ToString());
        }

        //检验录入时间
        if (jObject["checkdate"] != null && jObject["checkdate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["checkdate"].ToString());
            sql += string.Format(" AND scr.checkdate >= TO_DATE('{0} 00:00:00','yyyy-mm-dd hh24:mi:ss')", arr[0].ToString());
            sql += string.Format(" AND scr.checkdate <= TO_DATE('{0} 23:59:59','yyyy-mm-dd hh24:mi:ss')", arr[1].ToString());
        }

        return sql;
    }

    //查询方法
    Result getData(JObject jObject)
    {
        string sql = getSearchSqlString(jObject);
        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);
        DataTable dt = dto.DBTable;
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        Result r = new Result();
        r.data = dt;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //导出
    void exportExcel(JObject jObject)
    {
        string sql = getSearchSqlString(jObject);
        DataTable dtResult = dal.GetDataTable(sql);
        foreach (DataColumn col in dtResult.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }

        dtResult.Columns["productname"].Caption = "零件号";
        dtResult.Columns["productdescription"].Caption = "零件名称";
        dtResult.Columns["containername"].Caption = "批次号";
        dtResult.Columns["originalstartdate"].Caption = "开卡时间";
        dtResult.Columns["materialtrademark"].Caption = "材料牌号";
        dtResult.Columns["familylistnames"].Caption = "型别";
        dtResult.Columns["stepinfoname"].Caption = "工步";
        dtResult.Columns["workflowstepname"].Caption = "工序名称";
        dtResult.Columns["checkpointnotedis_b"].Caption = "检验内容";
        dtResult.Columns["drawingrequiredis_b"].Caption = "图纸规定";
        dtResult.Columns["checkpointresultnotedis_b"].Caption = "检验实测值";
        dtResult.Columns["checkdate"].Caption = "检验录入时间";
        dtResult.Columns["workflowtype"].Caption = "工艺类型";
        dtResult.Columns["process_no"].Caption = "工艺规程编号";
        dtResult.Columns["productrevision"].Caption = "工艺规程设计版次";
        dtResult.Columns["part_revid"].Caption = "设计图版次";
        dtResult.Columns["iskeycheck"].Caption = "是否关键过程";
        dtResult.Columns["specialcoursespecreportno"].Caption = "特殊过程确认编号";
        dtResult.Columns["confirmdate"].Caption = "特殊过程确认时间";
        dtResult.Columns["special_expire_date"].Caption = "特殊过程到期时间";
        dtResult.Columns["chargingqty"].Caption = "装炉量";
        dtResult.Columns["workflow_mark"].Caption = "工艺规程标识";
        dtResult.Columns["workflow_rev"].Caption = "工艺规程版次";
        dtResult.Columns["ischange"].Caption = "是否涉及工艺更改";
        dtResult.Columns["changeno"].Caption = "更改单号";
        dtResult.Columns["workflow_expire_date"].Caption = "临时工艺有效期";

        ExportHelper.ExportExcel(dtResult, "技术状态查询报表导出");
    }
}